<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>进货单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline" id="pageName"></div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" id="entityName" placeholder="请输入申请人/审核人/订单号" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    订单状态
                </div>
                <div class="layui-inline">
                    <select id="showCance" lay-filter="showCance">
                        <option value="-1">全部</option>
                        <option value="0">未审核</option>
                        <option value="1">已审核</option>
                        <option value="2">部分出库</option>
                        <option value="3">已完成</option>
                        <option value="9">已作废</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" id="entity_search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="entityTable" lay-filter="entityTable"></table>
        </div>
    </div>
</div>

<script type="text/html" id="table_bar">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="cance">作废</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </div>
</script>

<script src="../../js/jquery-1.8.3.min.js"></script>
<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    //首条件名称或写空""
    $("#pageName").text("");
    //路径配置 - 手动填写
    var findPageURL = "/purchaseOrder/findPage";
    var addURL = "/purchaseOrder/add";
    var toUpdateURL = "/purchaseOrder/findOne";
    var updateURL = "/purchaseOrder/update";
    var deleteURL = "/purchaseOrder/delete";
    var checkURL = "/purchaseOrder/check";

    var formTitle = "进货单";
    //页面配置 - 不用填写
    var entityType = "";
    var entityURL = "";
    var entityId = "";

    // 获取指定名称的cookie
    function getCookie(name) {
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
        //遍历匹配
        for (var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
            if (arr[0] == name) {
                return arr[1];
            }
        }
        return "";
    }


    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table', 'util'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , layer = layui.layer
            , util = layui.util
            , index = layui.index;

        //监听搜索
        $("#entity_search").on('click', function (data) {
                //执行重载
                var showCance = $('#showCance').val();
                table.reload('entityTable', {
                    method: 'post',
                    where: {
                        "ordernumber": $("#entityName").val(),
                        //"is_status": document.getElementById("showCance").checked ? 9 : 0
                        "is_status": showCance
                    },
                    page: {curr: 1}
                });
            }
        );

        //表格执行渲染
        table.render({
            elem: '#entityTable'
            , id: 'entityTable'
            , url: findPageURL
            , toolbar: true
            , page: true
            , limits: [10, 20, 30, 50]
            , cols: [[
                {fixed: 'left', field: 'ordernumber', title: '订单号', width: 160, sort: true}
                , {field: 'buycount', title: '进货数量', width: 90}
                , {field: 'price', title: '应付金额(元)', width: 130, sort: true}
                , {
                    field: 'is_status', title: '状态', width: 130, sort: true, templet: function (d) {
                        if (d.is_status == "0") return "<span class=\"layui-badge \" > 未审核 </span>\n"
                        else if (d.is_status == "1") return "<span class=\"layui-badge layui-bg-orange\" >已审核待入库</span>\n"
                        else if (d.is_status == "2") return "<span class=\"layui-badge layui-bg-blue\">部分已入库</span>\n"
                        else if (d.is_status == "3") return "<span class=\"layui-badge layui-bg-green\">已完成</span>\n"
                        else if (d.is_status == "9") return "<span class=\"layui-badge layui-bg-gray\">已作废</span>\n"
                    }
                }
                , {
                    field: 'is_pay', title: '支付状态', width: 100, sort: true, templet: function (d) {
                        if (d.is_pay == "0") return "<span class=\"layui-badge \" > 未支付 </span>\n"
                        else if (d.is_pay == "1") return "<span class=\"layui-badge layui-bg-orange\" >已支付</span>\n"
                    }
                }
                , {field: 'suname', title: '供应商', width: 90, sort: true}
                , {field: 'aname', title: '申请人', width: 90}
                , {field: 'rname', title: '审核人', width: 90}
                , {
                    field: 'gmt_create', width: 180, title: '创建时间', sort: true,
                    templet: function (d) {
                        return util.toDateString(d.gmt_create, 'yyyy-MM-dd HH:mm:ss')
                    }
                }
                , {
                    field: 'gmt_modified', width: 180, title: '修改时间', sort: true,
                    templet: function (d) {
                        return util.toDateString(d.gmt_modified, 'yyyy-MM-dd HH:mm:ss')
                    }
                }
                , {
                    fixed: 'right', title: '操作', width: 220, align: 'center', templet: function (d) {
                        var html = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>';
                        var role = getCookie("roleId");
                        if (d.is_status == "0") {
                            if (role == 1 || role == 2 || role == 3 || role == 7) {
                                html += '<a class="layui-btn layui-btn-xs" lay-event="check">审核</a>'
                                html += ' <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="cance">作废</a>'
                            }
                        }
                        // html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        return html;
                    }
                }
            ]]
            , where: {
                "ordernumber": $("#entityName").val(),
                //"is_status": document.getElementById("showCance").checked ? 9 : 0
                "is_status": $("#showCance").val()
            }
        });

        //添加操作
        var $ = layui.$, active = {
            add: function () {
                entityType = "add";
                entityURL = addURL;
                var layerAdd = layer.open({
                    type: 2
                    , title: "添加" + formTitle
                    , content: 'form.html'
                    , resize: false
                    , scrollbar: false
                    , area: ['850px', '500px']
                    , btn: false
                    , closeBtn: 0

                });
                layer.full(layerAdd);
            }
        };

        //监听行工具事件
        table.on('tool(entityTable)', function (obj) { //注：tool 是工具条事件名，zq_table 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (layEvent) {
                case 'del':
                    layer.confirm('您确定删除id：' + data.id + '的数据吗？', function (index) {
                        //向服务端发送删除指令，在这里可以使用Ajax异步
                        $.post(deleteURL, {id: data.id}, function (ret) {
                            layer.msg(ret.msg, {icon: ret.icon, time: 1500});
                            if (ret.status == "1") {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                                $(".layui-laypage-btn").click();//点击分页刷新当前页
                            }
                        });
                    });
                    break;
                case 'detail':
                    entityType = "toDetails";
                    entityURL = updateURL;
                    entityId = data.id;
                    var layerDetail = layer.open({
                        type: 2
                        , title: "查看" + formTitle
                        , content: 'form.html'
                        , resize: false
                        , area: ['850px', '500px']
                        , scrollbar: false
                        , btn: ['关闭']
                        , end: function () {
                            entityType = "";
                        }
                    });
                    layer.full(layerDetail);
                    break;
                case 'check':
                    layer.confirm('您确定要通过审核吗？', {icon: 3}, function (index) {
                        var load = layer.load(2);
                        $.get(checkURL, {"id": data.id, "is_status": 1}, function (ret) {
                            layer.close(load);
                            layer.msg(ret.msg, {icon: ret.icon, time: 1500});
                            if (ret.icon == 1) {
                                layer.close(index);
                                $(".layui-laypage-btn").click();//点击分页刷新当前页
                            }
                            layer.close(index);
                        })
                    });
                    break;
                case 'cance':
                    layer.confirm('您确定要作废该订单吗？', {icon: 3}, function (index) {
                        var load = layer.load(2);
                        $.get(checkURL, {"id": data.id, "is_status": 9}, function (ret) {
                            layer.close(load);
                            layer.msg(ret.msg, {icon: ret.icon, time: 1500});
                            if (ret.icon == 1) {
                                layer.close(index);
                                $(".layui-laypage-btn").click();//点击分页刷新当前页
                            }
                            layer.close(index);
                        })
                    });
                    break;
            }
        });
        //必备
        $('.layui-btn.layuiadmin-btn-role').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

